package main.java.com.example.demo.controller;

import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.example.demo.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @Param:
 * @Author:
 * @Date:
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 账号登录
     * @param user
     * @return
     */
    @PostMapping("/doLogin")
    public RespBean doLogin(@RequestBody User user){
        User userInfo = userService.findUser(user);
        if(userInfo.getId() == 0){
            return RespBean.error("5001", "账号或密码错误", null);
        }
        return RespBean.success("200", "登录成功", userInfo);
    }

    /**
     * 账号注册
     * @param user
     * @return
     */
    @PostMapping("/regUser")
    public RespBean regUser(@RequestBody User user){
        userService.insertUser(user);
        return RespBean.success("200", "注册成功", null);
    }

    /**
     * 查询全部用户
     */
    @GetMapping("/findAllUser")
    public RespBean findAllUser(){
        List<User> userList = userService.findAllUser();
        return RespBean.success("200", "查询成功", userList);
    }

    /**
     * 删除用户
     */
    @PostMapping("/delUser")
    public RespBean delUser(@RequestBody User user){
        userService.delUser(user);
        return RespBean.success("200", "删除成功", null);
    }
}
